package com.zlm.website.service.service.impl;

import com.zlm.common.base.AbstractBaseService;
import com.zlm.common.base.PageEntity;
import com.zlm.common.constants.CommonConstants;
import com.zlm.common.constants.NumberConstants;
import com.zlm.common.enums.NewsTypeEnum;
import com.zlm.website.service.dao.WebsiteNewsDao;
import com.zlm.website.service.entity.WebsiteNewsDO;
import com.zlm.website.service.service.WebsiteNewsService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 官网新闻实现类
 *
 * @Author: duyufu
 * @Date: 2019/3/14 14:45
 */
@Service
public class WebsiteNewsServiceImpl extends AbstractBaseService<WebsiteNewsDao, WebsiteNewsDO> implements WebsiteNewsService {

    @Autowired
    @Override
    public void setBaseDao(WebsiteNewsDao websiteNewsDao) {
        this.baseDao = websiteNewsDao;
    }

    @Override
    public List<WebsiteNewsDO> getEntityByType(Integer newsType) {
        return baseDao.getEntityByType(newsType);
    }

    @Override
    public List<WebsiteNewsDO> getNewsList(Map<String, Object> map) {
        return baseDao.getNewsList(map);
    }

    @Override
    public int countWebsiteNews(Integer newsType) {
        return baseDao.countWebsiteNews(newsType);
    }

    @Override
    public WebsiteNewsDO getEntityById(Integer id) {
        WebsiteNewsDO websiteNewsDO = baseDao.getEntityById(id);
        websiteNewsDO.setNewsTypeName(NewsTypeEnum.getNameByCode(websiteNewsDO.getNewsType()));
        return websiteNewsDO;
    }

    @Override
    public PageEntity queryAllWebsiteNews(Integer newsType, Integer pageNumber, Integer pageSize) {
        int count = baseDao.countWebsiteNews(newsType);
        if (null == pageNumber) {
            pageNumber = CommonConstants.pageNumber;
        }
        Map<String, Object> map = new HashMap<>(3);
        if (null == pageSize || pageSize >= CommonConstants.maxPageSize) {
            pageSize = CommonConstants.pageSize;
        }
        if (null == pageNumber || pageNumber.intValue() == NumberConstants.NUM_0) {
            pageNumber = CommonConstants.pageNumber;
        }else {
            pageNumber = (pageNumber-1) * pageSize;
        }
        map.put("newsType", newsType);
        map.put("pageNumber",pageNumber);
        map.put("pageSize", pageSize);
        List<WebsiteNewsDO> newsList = baseDao.getNewsList(map);
        for (WebsiteNewsDO websiteNewsDO : newsList) {
            websiteNewsDO.setNewsTypeName(NewsTypeEnum.getNameByCode(websiteNewsDO.getNewsType()));
        }
        PageEntity page = getPage(pageNumber,pageSize,count,newsList);
        return page;
    }

    @Override
    public boolean insertWebsiteNews(WebsiteNewsDO news) {
        int i = baseDao.insertWebsiteNews(news);
        if (i>0){
            return true;
        }
        return false;
    }

    @Override
    public List<WebsiteNewsDO> getWebsiteNews() {
        return baseDao.getWebsiteNews();
    }
}
